<template>
    <div class="goodsinfo-container">

        <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter" :max="59"
            >
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>
        


        <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<swiper :lunbotuList="lunbotu" :isfull="false"></swiper>
					</div>
				</div>
			</div>

            <div class="mui-card" >
				<div class="mui-card-header">{{goodsinfo.phoneModel}}</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
                        <p class="price">
                            市场价：<del>￥{{goodsinfo.oldPrice}}</del>&nbsp;&nbsp;销售价：<span class="now_price" >￥{{goodsinfo.nowPrice}}</span>
                        </p>
                        <p>购买数量:<numbox @getcount="getSelectedCount" :max="goodsinfo.number"></numbox>  </p>
						<mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
					</div>
				</div>
			</div>

			<div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<p>商品货号:89</p>
                        <p>库存情况:{{goodsinfo.number}}件</p>
                        <p>上架时间:2015-04-24 03:50:28</p>
					</div>
				</div>
				<div class="mui-card-footer">
                    <mt-button type="primary" plain size="large" @click="goDesc(id)">图文介绍</mt-button>
                    <mt-button type="danger" plain size="large" @click="goComment(id)">产品评论</mt-button>
                    <!-- 分析：如何实现加入购物车的时候，拿到 选择的数量
                    1.经过分析发现，按钮属于goodsinfo页面，数字属于numberbox 组件
                    2.由于涉及到父子组件的嵌套，所以，无法直接在goodsinfo中直接获取选中的商品数量值
                    3.怎么解决这个问题：涉及到子组件向父组件传值（事件调用 机制）
                    4.事件调用的本质：父向子传递方法，子调用这个方法，同时把数据当作参数，传递给这个方法 -->
                </div>
                
			</div>
			
    </div>
</template>
<script>
import swiper from '../subcomments/swiper.vue'
import numbox from '../subcomments/goodsinfo_numbox.vue'

export default({
    data(){
        return{
            id:this.$route.params.id,
            lunbotu:[],
            goodsinfo:[],
            ballFlag:false,
            SelectedCount:1
        }
    },
    created(){
        this.getLunBoTu()
        this.getGoodsInfo()
    },
    components:{
        swiper,
        numbox
    },
    methods:{
        getLunBoTu(){
            this.$http.get('http://localhost:3000/phoneImg').then(result=>{
                if(result.body){
                    result.body.forEach(item=>{
                        item.url=item.phone_src
                    })
                    this.lunbotu=result.body
                }
            })
        },
        getGoodsInfo(){
            this.$http.get('http://localhost:3000/goodslist').then(result=>{
                if(result.body){
                    this.goodsinfo=result.body[0]
                }
            })
        },
        goDesc(id){
            this.$router.push("/home/goodscomment/"+id)
        },
        goComment(id){
            this.$router.push("/home/goodsdesc/"+id)
        },
        addToShopCar(){
            this.ballFlag = !this.ballFlag
            var goodsinfo={id:this.id,count:this.SelectedCount,price:this.goodsinfo.nowPrice,imax:this.goodsinfo.number,selected:true}
            this.$store.commit("addToCar",goodsinfo)
        },
        beforeEnter(el){
            el.style.transform="translate(0,0)"
        },
        enter(el,done){
            el.offsetWidth
            //获取小球的位置
            const ballPosition = this.$refs.ball.getBoundingClientRect()
            //获取徽标的位置
            const badgePosition = document
            .getElementById("badge")
            .getBoundingClientRect()

            const xDist = badgePosition.left-ballPosition.left
            const yDist = badgePosition.top-ballPosition.top

            console.log(`translate(${xDist}}px,${yDist}px)`)
            
            el.style.transform=`translate(${xDist}px,${yDist}px)`
            // el.style.transform='translate(90px,233px)'
            el.style.transition="all 0.5s cubic-bezier(.3,-0.43,1,.73)"
            done()
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag
        },
        getSelectedCount(count){
            this.SelectedCount = count
            console.log("父组件拿到的数值是："+ this.SelectedCount)
        }
    }
})
</script>
<style lang="scss" scoped>
.goodsinfo-container{
    .mui-card-footer{
    display: block;
    button{
        margin: 15px 0;
    }
}
.now_price{
    font-size: 16px;
    color:red;
    font-weight: bold;
}
.ball{
    width: 15px;
    height: 15px;
    background-color: red;
    border-radius: 50%;
    position:absolute;
    z-index: 999;
    top:408px;
    left: 132px;
    
}
}


</style>
